package com.micro.service.sina;

import java.util.Map;

import android.content.Context;

import com.micro.constant.IGeneral;
import com.micro.util.CommunicatorResponse;
import com.micro.util.HttpCommunicator;

public class SinaCommHandler extends HttpCommunicator {
	private static final String API_KEY = "1906085257";

	public static final String CONSUMER_KEY_SINA = "1906085257";

	public static final String CONSUMER_SECRET_SINA = "5dfb9e4a4ce253061c3f4b8a8628e382";

	public static String userName;

	public static String password;

	private static Context context;

	private static String mService = IGeneral.SERVICE_NAME_SINA;

	public static Map trendParameterMap;

	public static void setTrendParameter(Map map) {
		SinaCommHandler.trendParameterMap = map;
	}

	public static void setAppContext(Context ctx) {
		context = ctx;
	}

	public static void setAccount(String userName, String password) {
		SinaCommHandler.userName = userName;
		SinaCommHandler.password = password;
	}

	public static CommunicatorResponse verifyUser(Map map) {

		CommunicatorResponse result = new CommunicatorResponse();

		String url = "http://api.t.sina.com.cn/account/verify_credentials.json";

		url = String.format(url + "?source=%s", API_KEY);

		String name = (String) map.get("accessToken");
		String passwd = (String) map.get("tokenSecret");

		result = httpGetOauth(mService, url, name, passwd);

		return result;
	}

	public static synchronized CommunicatorResponse getFriendsTimeLine(Map map) {
		CommunicatorResponse result = new CommunicatorResponse();
		String url = "http://api.t.sina.com.cn/statuses/friends_timeline.json?source="
				+ API_KEY + "&count=20";
		Object[] data = map.keySet().toArray();
		StringBuilder parameterBuilder = new StringBuilder();
		for (Object parameter : data) {
			parameterBuilder.append(String.format("&%s=%s", parameter,
					map.get(parameter)));
		}
		result = httpGetOauth(mService, url + parameterBuilder.toString(),
				userName, password);
		return result;
	}
}
